package com.approval.approvalapp.center.acitivty;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approval.QjkgAdapter2;
import com.approval.approval.QjkgBean;
import com.approval.approvalapp.MyAppliaction;
import com.approval.approvalapp.R;
import com.approval.approvalapp.center.adapter.QjkgAdapter;
import com.approval.approvalapp.center.bean.QjkgBena;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QjkgActivity extends BaseActivity {

    @Bind(R.id.tv_empty)
    TextView tvEmpty;
    @Bind(R.id.empty_ll)
    LinearLayout emptyLl;
    private boolean isCenter;
    @Bind(R.id.recyclerView2)
    RecyclerView recyclerView2;

    public static void goInto(Context context, String text, boolean isCenter) {
        Intent intent = new Intent();
        intent.setClass(context, QjkgActivity.class);
        intent.putExtra("text", text);
        intent.putExtra("isCenter", isCenter);
        context.startActivity(intent);
    }

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    List<QjkgBena> mList = new ArrayList<>();
    @Bind(R.id.tv_title_time)
    TextView tvTitleTime;
    @Bind(R.id.tv_num_ber)
    TextView tvNumBer;
    private QjkgAdapter adapter;
    private QjkgAdapter2 adapter2;


    @Override
    public int getLayoutId() {
        return R.layout.qjkg_revise_layout;
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        tvTitleTime.setText(simpleDateFormat.format(date));

        tvNumBer.setText(getIntent().getStringExtra("text"));

        isCenter = getIntent().getBooleanExtra("isCenter", false);

        if (isCenter) {
            titleTv1.setText("请假/旷工（中心人员）");
            tvEmpty.setText("今日中心人员无请假/旷工");
        } else {
            titleTv1.setText("请假/旷工（窗口人员）");
            tvEmpty.setText("今日窗口人员无请假/旷工");
        }

        getData();
    }

    public void getData() {
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("org_code", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.ORG_CODE, "-1"));
//        jsonObject.put("region_code", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.REGION_CODE, "-1"));
//        jsonObject.put("areacode", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.REGION_CODE, "-1"));
        if (isCenter) {
            jsonObject.put("is_select", "1" );
        } else {
            jsonObject.put("is_select", "2" );
        }
        jsonObject.put("areacode", SpUtil.getString(this, SpUtil.REGION_CODE, ""));
        Log.d("QjkgActivity1", jsonObject.toString());
        Log.d("QjkgActivity1", SpUtil.getString(this, SpUtil.USER_RUN_ID, ""));
        Log.d("QjkgActivity1", SpUtil.getString(this, SpUtil.F_userid, ""));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/leave_absenteeism_list",
                jsonObject.toString(), new NetDownResponse() {

                    @Override
                    public void success(String str) {
                        QjkgBean qjkgBean = JSON.parseObject(str, QjkgBean.class);
                        List<QjkgBean.LeaveTodayBean> leave_today = qjkgBean.getLeave_today();
                        List<QjkgBean.AbsenteeismTodayBean> absenteeism_today = qjkgBean.getAbsenteeism_today();


                        if (leave_today.size() == 0 && absenteeism_today.size() == 0) {
                            emptyLl.setVisibility(View.VISIBLE);
                            recyclerView.setVisibility(View.GONE);
                        } else {
                            emptyLl.setVisibility(View.GONE);
                            recyclerView.setLayoutManager(new LinearLayoutManager(QjkgActivity.this));
                            recyclerView2.setLayoutManager(new LinearLayoutManager(QjkgActivity.this));
                            adapter = new QjkgAdapter();
                            adapter.setAbsenteeism_today(absenteeism_today);
                            recyclerView.setAdapter(adapter);
                            adapter2 = new QjkgAdapter2(leave_today, QjkgActivity.this);
                            recyclerView2.setAdapter(adapter2);
                            adapter2.notifyDataSetChanged();
                        }

                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {
                        if (swipeRefreshLayout != null) {
                            swipeRefreshLayout.setRefreshing(false);
                        }
                    }
                });

    }

    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }


}